import { OneModal } from '@/components/one-ui';
import { IOneModalRef } from '@/pages/type';
import { Ref, useContext, useEffect, useMemo, useRef, useState } from 'react';
import { AssetDiscardContext, IAcquisitionActionEnum } from '../mconfig';
import { materialTableService } from '../services';
import { materialTableConfig } from '../mconfig';
import { Col, Row, message } from 'antd';
import { getColorTextLoadMap, getKeyRouter, getLoadMapList, getSelectDataMapList, getTenantID, onCloseModalCommon } from '@/utils';
import { showTotalInfo } from '@/common/constVariable';
import { UpdateTableCommon } from './UpdateTableCommon';
import { ILoadMapTypeAll } from '@/common';

export const OpenMaterialModal: React.FC<any> = ({ onClose, isShowModal }) => {
  /**弹框的ref*/
  const updateModalRef: Ref<IOneModalRef> = useRef(null);
  const styleFixCls = 'contract-styfixcls-';
  // /**  获取数据字典 */
  const { loadMapInfo, mapLoading } = getLoadMapList([
    `${getTenantID()}${ILoadMapTypeAll.ASSET_TYPE}`, //资产类型
  ]);
  // /** 资产类型 */
  const assetTypeList = getSelectDataMapList(loadMapInfo?.[`${getTenantID()}${ILoadMapTypeAll.ASSET_TYPE}`], 'name', 'id', true);

  /** 获取上下文 */
  const {
    materielList,
    curRes,
    rightTableProps: { materielForm },
    acquisitionDispatch,
  } = useContext(AssetDiscardContext);
  /** 获取列表 */
  const { data, params, loading, pagination, searchList, loadMdicMapObj, getManageData } = materialTableService(curRes);

  /** 暂时保存选中的值 */
  const [checkMaterialList, setCheckMaterialList] = useState<Record<string, any>[]>([]);

  /** 搜索 */
  const onSearch = (payload: Record<string, any>) => {
    getManageData({ ...payload });
  };

  /** 重置 */
  const onReset = (payload: Record<string, any>) => {
    getManageData({ ...payload });
  };

  /** 分页change 方法 */
  const onPageChange = (current: number, pageSize: number) => {
    getManageData({ ...params, current, pageSize });
  };

  /** 点击选中 */
  const onSelectTable = (selectInfo) => {
    setCheckMaterialList(selectInfo);
  };

  /** 初始化 */
  useEffect(() => {
    setCheckMaterialList(materielList);
  }, [isShowModal]);

  /** 点击确定 */
  const onSave = () => {
    if (checkMaterialList && checkMaterialList?.length > 0) {
      // materielForm.setFieldsValue({
      //   detailList: checkMaterialList,
      // });
      // 存储到仓库
      acquisitionDispatch({
        type: IAcquisitionActionEnum.SELECT_MATERIEL,
        payload: [...checkMaterialList],
      });
      onCloseModalCommon(updateModalRef);
    } else {
      message.error('请至少选择一个资产');
    }
    return;
  };

  return (
    <>
      <OneModal
        open={true}
        getContainer={false}
        ref={updateModalRef}
        title={'资产列表'}
        onCancel={onClose}
        width={document.body.clientWidth * 0.65}
        bodyStyle={{
          height: document.body.clientHeight * 0.55,
          padding: 10,
        }}
        onOk={onSave}
      >
        <Row className={`${styleFixCls}materialModal`}>
          <Col>
            <UpdateTableCommon
              styleFixCls={styleFixCls}
              searchProps={{
                searchList,
                onReset,
                onSearch,
              }}
              selectTablePorps={{
                onSelectTable,
                selectRowObjList: checkMaterialList,
                selectType: 'checkbox',
                setCheckRows: setCheckMaterialList,
              }}
              tableProps={{
                columns: materialTableConfig(),
                loading,
                dataSource: data?.rows,
                pagination,
                onPageChange,
                rowKey: 'id',
                tableTotalShow: (total: any, range: any) => {
                  return (
                    <div
                      style={{
                        display: 'flex',
                        width: '100%',
                        justifyContent: 'space-between',
                      }}
                    >
                      <div>
                        已选择<strong style={{ color: 'red' }}> {checkMaterialList?.length} </strong>条资产
                      </div>
                      <div>{showTotalInfo(total, range)}</div>
                    </div>
                  );
                },
              }}
            />
          </Col>
        </Row>
      </OneModal>
    </>
  );
};
